package com.shrct.businessprocess.purchase.impl;

import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.purchase.RCBasePurchaseOrder;
import com.shrct.businessprocess.purchase.RCBasePurchaseRequest;
import com.shrct.businessprocess.purchase.RCHasPurchaseBudget;
import com.shrct.businessprocess.purchase.RCProductPurchaseRequestStatus;
import com.shrct.businessprocess.purchase.RCPurchaseRequestProcess;
import com.shrct.businessprocess.purchase.RCSupplierPriceQuery;
import com.shrct.businessprocess.purchase.impl.RCHasPurchaseBudgetImpl;
import com.shrct.businessprocess.purchase.impl.RCSupplierPriceQueryImpl;
import com.shrct.businessprocess.purchase.proxy.RCBasePurchaseRequestProxy;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IGenericQuery;
import java.util.Collection;
import java.util.Date;
import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCBASEPURCHASEREQUEST_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcbasepurchaserequest")
public abstract class RCBasePurchaseRequestImpl extends BaseModelObject implements RCBasePurchaseRequest {
  public RCBasePurchaseRequestImpl() {
    super();
  }
  
  public RCBasePurchaseRequestImpl(final RCHasPurchaseBudget budget) {
    super();
    this.budget = budget;
  }
  
  private Date date;
  
  public Date getDate() {
    return this.date;
  }
  
  public RCBasePurchaseRequest setDate(final Date date) {
    this.date = date;
    return this;			
    
  }
  
  private Date expectDate;
  
  public Date getExpectDate() {
    return this.expectDate;
  }
  
  public RCBasePurchaseRequest setExpectDate(final Date expectDate) {
    this.expectDate = expectDate;
    return this;			
    
  }
  
  @Embedded
  @AttributeOverrides(value = { @AttributeOverride(name = "amount", column = @Column(name = "expectCost_amount") ), @AttributeOverride(name = "workaround", column = @Column(name = "expectCost_workaround") ) } )
  @AssociationOverrides(value = @AssociationOverride(name = "exchange", joinColumns = @JoinColumn(name = "expectCost_exchange") ) )
  private RCProjectMoney expectCost = com.shrct.businessprocess.project.RCProjectMoney.create();
  
  public RCProjectMoney getExpectCost() {
    return this.expectCost;
  }
  
  public RCBasePurchaseRequest setExpectCost(final RCProjectMoney expectCost) {
    this.expectCost = expectCost;
    return this;			
    
  }
  
  private Boolean isPassed = Boolean.valueOf(false);
  
  public Boolean getIsPassed() {
    return this.isPassed;
  }
  
  public RCBasePurchaseRequest setIsPassed(final Boolean isPassed) {
    this.isPassed = isPassed;
    return this;			
    
  }
  
  private Boolean isFinished = Boolean.valueOf(false);
  
  public Boolean getIsFinished() {
    return this.isFinished;
  }
  
  public RCBasePurchaseRequest setIsFinished(final Boolean isFinished) {
    this.isFinished = isFinished;
    return this;			
    
  }
  
  private RCProductPurchaseRequestStatus status;
  
  public RCProductPurchaseRequestStatus getStatus() {
    return this.status;
  }
  
  public RCBasePurchaseRequest setStatus(final RCProductPurchaseRequestStatus status) {
    this.status = status;
    return this;			
    
  }
  
  @Column(length = 1024)
  private String comment;
  
  public String getComment() {
    return this.comment;
  }
  
  public RCBasePurchaseRequest setComment(final String comment) {
    this.comment = comment;
    return this;			
    
  }
  
  @JoinColumn(name = "budget_id")
  @ManyToOne(targetEntity = RCHasPurchaseBudgetImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCHasPurchaseBudget budget;
  
  public RCHasPurchaseBudget getBudget() {
    return this.budget;
  }
  
  public RCBasePurchaseRequest setBudget(final RCHasPurchaseBudget budget) {
    this.budget = budget;
    return this;			
    
  }
  
  @OneToMany(targetEntity = RCSupplierPriceQueryImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "purchaseRequest")
  private Collection<RCSupplierPriceQuery> priceQueries = new java.util.ArrayList<com.shrct.businessprocess.purchase.RCSupplierPriceQuery>();;
  
  public Collection<RCSupplierPriceQuery> getPriceQueries() {
    return this.priceQueries;
  }
  
  public RCSupplierPriceQuery createAndAddtoPriceQueries() {
    getPriceQueries().size();
    com.shrct.businessprocess.purchase.RCSupplierPriceQuery rCSupplierPriceQuery = new com.shrct.businessprocess.purchase.impl.RCSupplierPriceQueryImpl(this);
    getObjectFactory().create(rCSupplierPriceQuery);
    getPriceQueries().add(rCSupplierPriceQuery);
    return rCSupplierPriceQuery;
    
  }
  
  public RCSupplierPriceQuery createAndAddtoPriceQueries(final Procedure1<RCSupplierPriceQuery> updater) {
    getPriceQueries().size();
    com.shrct.businessprocess.purchase.RCSupplierPriceQuery rCSupplierPriceQuery = new com.shrct.businessprocess.purchase.impl.RCSupplierPriceQueryImpl(this);
    getObjectFactory().create(rCSupplierPriceQuery);
    getPriceQueries().add(rCSupplierPriceQuery);
    return rCSupplierPriceQuery;
    
  }
  
  public RCBasePurchaseRequest removeAllPriceQueries() {
    for(RCSupplierPriceQuery obj : this.priceQueries){
    	getObjectFactory().delete(obj);
    }
    this.priceQueries.clear();
    return this;	
    
  }
  
  public RCBasePurchaseRequest removeFromPriceQueries(final RCSupplierPriceQuery rCSupplierPriceQuery) {
    this.priceQueries.remove(rCSupplierPriceQuery);
    getObjectFactory().delete(rCSupplierPriceQuery);
    return this;
    
  }
  
  public RCProjectMoney getTotalRequestedAmount() {
    return this.expectCost;
  }
  
  public RCExchangeRate getExchangeRate() {
    RCExchangeRate _defaultPurchaseExchangeRate = this.budget.getDefaultPurchaseExchangeRate();
    return _defaultPurchaseExchangeRate;
  }
  
  public RCPurchaseRequestProcess getProcess() {
    IGenericQuery<RCPurchaseRequestProcess> query=getObjectFactory().createGenericQuery(RCPurchaseRequestProcess.class,"select p from com.shrct.businessprocess.purchase.impl.RCPurchaseRequestProcessImpl as p   where ((p.purchaseRequest) = (:this)) and  p.active = 1      ").setParameter("this",this);
    RCPurchaseRequestProcess _uniqueResult = query.uniqueResult();
    return _uniqueResult;
  }
  
  public void postCancel() {
    Collection<? extends RCBasePurchaseOrder> _purchaseOrders = this.getPurchaseOrders();
    int _size = _purchaseOrders.size();
    boolean _equals = (_size == 0);
    if (_equals) {
      this.isPassed = Boolean.valueOf(false);
      this.status = RCProductPurchaseRequestStatus.canceled;
    } else {
      RuntimeException _runtimeException = new RuntimeException("invalid state: purchase order created!");
      throw _runtimeException;
    }
  }
  
  public RCProject getProject() {
    RCCostBreakDown _cbd = this.budget.getCbd();
    RCProject _project = _cbd.getProject();
    return _project;
  }
  
  public RCBasePurchaseRequestProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCBasePurchaseRequestProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCBasePurchaseRequestProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
